# -*- coding: utf-8 -*-
########################################################################
#   Pax-Selector. Copyright 2010, Hector Sanjuán.                      #
#                                                                      #
#   Pax-selector is free software: you can redistribute it and/or      #
#   modify it under the terms of the GNU General Public License as     #
#   published by the Free Software Foundation, either version 3 of     #
#   the License, or any later version.                                 #
#                                                                      #
#   Pax-selector is distributed in the hope that it will be useful,    #
#   but WITHOUT ANY WARRANTY; without even the implied warranty of     #
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      #
#   GNU General Public License for more details.                       #
#                                                                      #
#   You should have received a copy of the GNU General Public License. #
#   If not, see <http://www.gnu.org/licenses/>.                        #
#                                                                      #
########################################################################

class PaxSelector
  module Views
    class Ranking < Mustache

      def info 
        @stats
      end
      
      def fields
        r = "<th>#</th><th>ID</th>"
        @list.rankfields.each do | field |
          r+="<th>#{field}</th>\n"
        end
        
        r+="<th>Final Mark</th>\n"
        r+="<th>comments</th>"
        return {:fieldnames => r}
      end
      
      def participants
        tablefields = []
        rankfields = ""
        ranking = @rankingList.ranking
        @list.each do |participant|
          id = participant[:id]
          rankItem = ranking[id]
          rankMark = rankItem ? rankItem[:rankMark] : 0
          barMark = rankItem ? rankItem[:barMark] : 0
          comments = rankItem ? rankItem[:comments].join('<br /><br />') : "No comments"
          comments = "No Comments" if comments.empty?
          comments = "<span class=\"comments\">"+comments+"</span>"
          values = ""
          @list.rankfields.each do | field |
            values+="<td>#{participant[field]}</td>\n"
          end
          
          field = 
            {
            :row => 
              "<td><a href=\"/list/#{id}\">#{id}</a></td>"+
                values+
               "<td><div class=\"mark\"><div id=\"value\">#{rankMark}</div>
                                        <div class=\"rankingbar\">#{barMark}</div></div></td>
                <td class=\"comments\">#{comments}</td>
	      </tr>",
            :rankMark => rankMark
          }
          tablefields << field
        end
        tablefields.sort! {|x,y| -(x[:rankMark] <=> y[:rankMark])}
        count = 1
        tablefields.each do | elem |
          elem[:row] = "<tr><td>#{count}</td>" + elem[:row]
          count += 1
        end
      end
    end
  end
end
